package com.tj.view.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.tj.view.R;
import com.tj.view.view.FlyView;
import com.tj.view.view.MotionEventTest;

import butterknife.Bind;

/**
 * Created by Administrator on 2017/7/17 0017.
 *
 * 会飞的view
 *
 * 关键是view的中心到另一个view的中心,
 *
 * mImgReceive.getX()+mImgReceive.getPivotX()-(mImgSend.getX()+mImgSend.getPivotX())
 *
 */
public class FlyAnimatorActivity extends BaseActivity {

    private static final String TAG = "FlyAnimatorActivity";

    @Bind(R.id.img_send)
    MotionEventTest mImgSend;
    @Bind(R.id.img_receive)
    ImageView mImgReceive;

    int xSend = 0;
    int ySend = 0;
    int xReceive = 0;
    int yReceive = 0;
    private float mIconHeight;
    private float mIconWidth;
    public static final float SCALE_OFFSET = 0.5f;//这个缩放的偏移量？？？
    private float mScale;// 缩小的比例

    // 视图内容对象
    private ImageView mContentImageView;
    // 容器布局
    private FrameLayout mContainerFrameLayout;

    @Override
    public int getContentViewId() {
        return R.layout.layout_fly_animator;
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        //view的getx，gety获取是view左上角到父控件的距离
        //view的getPivotX,getPivotY获取是view的宽一半，高一半的值
        int[] location = new int[2];
        mImgSend.getLocationOnScreen(location);
        xSend = location[0];
        ySend = location[1];
        Log.d(TAG, "onClick:屏幕位置mImgSend x="+xSend+",y="+ySend);

        int[] location2 = new int[2];
        mImgReceive.getLocationOnScreen(location2);
        xReceive = location2[0];
        yReceive = location2[1];
        Log.d(TAG, "onClick:屏幕位置mImgReceive x="+xReceive+",y="+yReceive);

        mIconHeight = mImgSend.getHeight();
        mIconWidth = mImgSend.getWidth();


    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setContainer();
    }

    FlyView mFlyView;
    @Override
    public void initData() {

        mImgSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mFlyView = new FlyView(FlyAnimatorActivity.this);
                mFlyView.initByView(mImgSend, mImgReceive);
                mFlyView.startFly();

//                performAnim();
//                preTest();
            }
        });

    }

    public void preTest(){
        //平移x, 关键是view的中心到另一个view的中心
        PropertyValuesHolder valuesHolder1 = PropertyValuesHolder.ofFloat("translationX",
                xReceive+mImgReceive.getPivotX()-(xSend+mImgSend.getPivotX()));
        //平移y
        PropertyValuesHolder valuesHolder2 = PropertyValuesHolder.ofFloat("translationY",
                yReceive+mImgReceive.getPivotY()-(ySend+mImgSend.getPivotY()));
        PropertyValuesHolder valuesHolder3 = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.5f);
        PropertyValuesHolder valuesHolder4 = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.5f);
        PropertyValuesHolder valuesHolder5 = PropertyValuesHolder.ofFloat("rotationX", 0.0f, 360.0f);
        PropertyValuesHolder valuesHolder6 = PropertyValuesHolder.ofFloat("rotationY", 0.0f, 360.0f);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mImgSend,
                valuesHolder1, valuesHolder2,valuesHolder3,valuesHolder4,valuesHolder5,valuesHolder6);
        objectAnimator.setDuration(3000).start();
    }

    // 设置容器，装载动画的view
    private void setContainer() {
        WindowManager mWm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        // 不允许触摸获取焦点
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        wmParams.gravity= Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角

        // 设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        //以屏幕左上角为原点，设置x、y初始值
        wmParams.x = 0;
        wmParams.y = 0;
        // 宽高适应内容
        wmParams.width = 90;
        wmParams.height = 90;
        // 如果view没有被加入到某个父组件中，则加入WindowManager中

        mContentImageView = new ImageView(this);
        mContentImageView.setImageDrawable(getDrawable(R.drawable.ic_launcher));
        // 如果view没有被加入到某个父组件中，则加入WindowManager中
        mWm.addView(mContentImageView, wmParams);
    }

    // 执行动画
    private void performAnim() {

        mScale = computeScale(mImgSend, mImgReceive);

        // 移动，旋转，缩放，透明度
        AnimatorSet allAnimSet = new AnimatorSet();
        // 1.旋转
        ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(
                mImgSend, "rotation", 1080);
        // 2.移动X
        ObjectAnimator transXAnimator = ObjectAnimator.ofFloat(
                mImgSend, "translationX",  xReceive- xSend
                        - ((1 - mScale) * mIconWidth) / 2);
        // 3.移动Y
        ObjectAnimator transYAnimator = ObjectAnimator.ofFloat(
                mImgSend, "translationY", yReceive - ySend
                        - ((1 - mScale) * mIconHeight) / 2);
        // 4.缩放X
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(
                mImgSend, "scaleX", mScale * SCALE_OFFSET);
        // 5.缩放Y
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(
                mImgSend, "scaleY", mScale * SCALE_OFFSET);

        // 6.透明度
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(
                mImgSend, "alpha", 0.2f);

        allAnimSet.playTogether(rotationAnimator, transXAnimator,
                transYAnimator, scaleXAnimator, scaleYAnimator, alphaAnimator);
        allAnimSet.setDuration(1000).start();
    }

    /**
     * 计算缩放比例 <功能简述>
     */
    private float computeScale(View oriView, View desView) {
        float scaleH = ((float) desView.getHeight())
                / ((float) oriView.getHeight());
        float scaleW = ((float) desView.getWidth())
                / ((float) oriView.getWidth());
        return scaleH < scaleW ? scaleH : scaleW;
    }


}
